package cn.rongdao.jrkabao.ui.main.mainfragment.uicomponent;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mifengkong.frtools.util.FRImageLoad;

import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.ui.message.MessageActivity;
import cn.rongdao.jrkabao.ui.webview.WebViewActivity;
import cn.rongdao.jrkabao.util.LoginUtil;
import cn.rongdao.jrkabao.util.growingio.BuriedUtil;

/**
 * Created by jack on 2017/9/8
 */

public class CashTitleBarUIComponent extends RelativeLayout {
    public CashTitleBarUIComponent(Context context) {
        super(context);
    }

    public CashTitleBarUIComponent(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CashTitleBarUIComponent(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private ImageView ivMessage;
    private TextView title;
    private ImageView rightImg;

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        ivMessage = findViewById(R.id.fca_iv_message);
        title = findViewById(R.id.fca_tv_title);
        rightImg = findViewById(R.id.fca_iv_right_img);
        ivMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BuriedUtil.kamao_shouye_fenleiye_apply(getContext());
                if (!LoginUtil.isLogin(getContext())) {
                    return;
                }
                MessageActivity.start(getContext());
//                WebViewActivity.start(getContext(),"file:///android_asset/hello.html");
//                WebViewActivity.start(getContext(),"http://local-test.com/m-examples-fetch");
//                WebViewActivity.start(getContext(),"http://demo.h5-zakas.mifengkong.cn/m-applycredit-home");
//                TestActivity.start(getContext());
            }
        });
    }

    public void setWhiteTitle(String titleText) {
        title.setText(titleText);
    }

    public void setIsHaveMessage(boolean isHaveMsg) {
        ivMessage.setImageResource(isHaveMsg ? R.drawable.icon_have_message : R.drawable.icon_no_message);
    }

    public void setTitleRightVisible(boolean isVisible) {
        if (isVisible) {
            rightImg.setVisibility(VISIBLE);
        } else {
            rightImg.setVisibility(GONE);
        }
    }

    //设置右边的图标
    public void setTitleRightImg(String imgUrl) {
        if (TextUtils.isEmpty(imgUrl)) {
            return;
        }
        String splitStr = imgUrl.substring(imgUrl.length() - 3, imgUrl.length());
        if ("gif".equals(splitStr)) {
            FRImageLoad.loadImageGif(imgUrl, rightImg);
        } else {
            FRImageLoad.loadImage(imgUrl, rightImg);
        }
    }

    //设置右边的图标的点击事件
    public void setRightImgOnClickLister(final String linkUrl) {
        if (TextUtils.isEmpty(linkUrl)) {
            return;
        }
        rightImg.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!LoginUtil.isLogin(getContext())) {
                    return;
                }
                WebViewActivity.start(getContext(), linkUrl);
            }
        });
    }

    public void setMessageVisible(boolean isVisible) {
        ivMessage.setVisibility(isVisible ? VISIBLE : GONE);
    }
}
